export function chooseMedia() {
  return new Promise((resolve, reject) => {
    wx.chooseMedia({
      count: 1,
      mediaType: ["image"],
      sourceType: ["camera"],
      sizeType:['compressed'],
      camera: 'back',
      async success(res) {
        try{
          var r = await invokeService(res.tempFiles[0].tempFilePath);
        }catch(err){
          reject(err);
          return ;
        }
        r.tempFilePath=res.tempFiles[0].tempFilePath;
        resolve(r);
      },
      fail(err){
        reject(err)
      }
    })
  });
}

export async function invokeService(res) {
  const invokeRes = await wx.serviceMarket.invokeService({
    service: 'wx79ac3de8be320b71',
    api: 'OcrAllInOne',
    data: {
      // 用 CDN 方法标记要上传并转换成 HTTP URL 的文件
      img_url: new wx.serviceMarket.CDN({
        type: 'filePath',
        filePath: res,
      }),
      data_type: 3,
      ocr_type: 1
    },
  })
  console.log('invokeService success', invokeRes)
  return {
    idcard_res: invokeRes.data.idcard_res
  };
}